package com.zn.util;


import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yinjiawei on 2018/8/13.
 */
public class IdCardValidate {
    private static final Logger LOGGER = LoggerFactory.getLogger(IdCardValidate.class);
//    private static String HOST="https://idcert.market.alicloudapi.com";
//    private static String PATH="/idcard";
//    private static String APPCODE="ce321b49529e49678674601f154ad872";
//    private static String METHOD="GET";

    public static String validate(String idCard,String name,String host,String path,String appcode,String method) throws Exception{
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("idCard", idCard);
        querys.put("name", name);
        HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
//        LOGGER.info(EntityUtils.toString(response.getEntity()));
        String jsonStr=EntityUtils.toString(response.getEntity());
        LOGGER.info(jsonStr);
        return jsonStr;
    }

    public static void main(String[] args) {
        String host = "https://idcert.market.alicloudapi.com";
        String path = "/idcard";
        String method = "GET";
        String appcode = "422830de3e0f467ebad2f7760ab95fc3";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("idCard", "430481199606296994");
        querys.put("name", "尹佳威");
        //JDK 1.8示例代码请在这里下载：  http://code.fegine.com/Tools.zip


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 或者直接下载：
             * http://code.fegine.com/HttpUtils.zip
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             * 相关jar包（非pom）直接下载：
             * http://code.fegine.com/aliyun-jar.zip
             */
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            //LOGGER.info(response.toString());
            //获取response的body
            LOGGER.info(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
